package com.hegongda.okcmall.product.service.impl;

import com.hegongda.okcmall.product.entity.AttrEntity;
import com.hegongda.okcmall.product.service.AttrService;
import com.hegongda.okcmall.product.vo.Attr;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hegongda.common.utils.PageUtils;
import com.hegongda.common.utils.Query;

import com.hegongda.okcmall.product.dao.SkuSaleAttrValueDao;
import com.hegongda.okcmall.product.entity.SkuSaleAttrValueEntity;
import com.hegongda.okcmall.product.service.SkuSaleAttrValueService;


@Service("skuSaleAttrValueService")
public class SkuSaleAttrValueServiceImpl extends ServiceImpl<SkuSaleAttrValueDao, SkuSaleAttrValueEntity> implements SkuSaleAttrValueService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuSaleAttrValueEntity> page = this.page(
                new Query<SkuSaleAttrValueEntity>().getPage(params),
                new QueryWrapper<SkuSaleAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(Long skuId, List<Attr> attrs) {
        if (attrs != null && attrs.size() > 0){
            List<SkuSaleAttrValueEntity> collect = attrs.stream().map(attr -> {
                SkuSaleAttrValueEntity skuSaleAttrValueEntity = new SkuSaleAttrValueEntity();
                BeanUtils.copyProperties(attr, skuSaleAttrValueEntity);
                skuSaleAttrValueEntity.setSkuId(skuId);
                AttrEntity attrEntity = attrService.selectOne(attr.getAttrId());
                if (attrEntity != null) {
                    skuSaleAttrValueEntity.setAttrName(attrEntity.getAttrName());
                }
                return skuSaleAttrValueEntity;
            }).collect(Collectors.toList());
            this.saveBatch(collect);
        }
    }

}